package com.fengcone.lucene.crawler;

import java.io.PrintWriter;

import org.apache.http.client.fluent.Request;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class ZimuzuCrawler {
	private static final String ZIMUZU_URL = "http://www.zimuzu.tv/subtitle/";
	private static final String FILE_PATH = "D:\\WorkSpace\\WorkSpace_SE\\caption.file\\";
	private static final int MAX_ID = 49000;

	public static void main(String[] args) throws Exception {
		PrintWriter writer = new PrintWriter(FILE_PATH + MAX_ID + ".txt");
		for (int i = MAX_ID; i > 0; i--) {
			String html = getHtmlContent(i);
			if (html == null) {
				continue;
			}
			String downloadUrl = getDownloadUrl(html);
			if (downloadUrl == null) {
				continue;
			}
			System.out.println(i + "---->" + downloadUrl);
			writer.println(downloadUrl);
			if (i % 1000 == 0) {
				writer.flush();
				writer.close();
				writer = new PrintWriter(FILE_PATH + i + ".txt");
			}
		}
		writer.flush();
		writer.close();
	}

	private static String getDownloadUrl(String html) {
		try {
			Document document = Jsoup.parse(html);
			Elements elements = document.body().getElementsByTag("h3");
			String element = elements.get(1).child(0).attr("href");
			return element;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static String getHtmlContent(int id) {
		try {
			return Request.Get(ZIMUZU_URL + id).execute().returnContent().asString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
